package com.yhjapp.onethingoneyard.onethingoneyard;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.squareup.okhttp.Request;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Map;

import butterknife.OnClick;

/**
 * @author: wangZ
 * @date：2017/8/17 16:51
 * FileName：LoginActivity
 * @description：描述登录页面
 */

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private String mOpenid;

    private String mUnionId;
    private String mName;
    private String mAvatr;
    private TextView tv_SMS_quick_login;
    private TextView tv_Forgot_password_login;
    private TextView tv_login_Register;



    private EditText et_Login_phone;
    private EditText et_login_pwd;
    private TextView tv_login;
//    private ClickImageView tv_qq_login;
//    private ImageView image;
    boolean eyeOpen = false ;


    private ImageView iv_login_back;
    private ImageView iv_login_colse;
    private String phone;
    private String pwd;
    private String phonew;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        tv_SMS_quick_login= (TextView) findViewById(R.id.tv_SMS_quick_login);
        tv_SMS_quick_login.setOnClickListener(this);
        tv_Forgot_password_login= (TextView) findViewById(R.id.tv_Forgot_password_login);
        tv_Forgot_password_login.setOnClickListener(this);
        tv_login_Register= (TextView) findViewById(R.id.tv_login_Register);
        tv_login_Register.setOnClickListener(this);
//        image= (ImageView) findViewById(R.id.image_login);
        et_login_pwd= (EditText) findViewById(R.id.et_login_pwd);
        et_Login_phone= (EditText) findViewById(R.id.et_Login_phone);


        String phone=getIntent().getStringExtra("phone");
        if("".equals(phone)){

        }else{
            et_Login_phone.setText(phone);
        }


        tv_login= (TextView) findViewById(R.id.tv_login_l);
        tv_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phonew = et_Login_phone.getText().toString();
                pwd = et_login_pwd.getText().toString();
                Contig.pwd=pwd;
                Contig.phone= phonew;
                SPUtils.getInstance().put(SPConstants.PHONE,Contig.phone);
                SPUtils.getInstance().put(SPConstants.PWD,Contig.pwd);
                Log.e("",""+SPUtils.getInstance().getString(SPConstants.PHONE)+"    SSS       "+SPUtils.getInstance().getString(SPConstants.PWD));
                LoadLogin();
            }
        });
        iv_login_back= (ImageView) findViewById(R.id.iv_login_back);
        iv_login_colse= (ImageView) findViewById(R.id.iv_login_colse);
        iv_login_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        iv_login_colse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        image.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if ( eyeOpen ){
//                    //密码 TYPE_CLASS_TEXT 和 TYPE_TEXT_VARIATION_PASSWORD 必须一起使用
//                    et_login_pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//                    image.setImageResource( R.drawable.login_icon_bukejian );
//                    eyeOpen = false ;
//                }else {
//                    //明文
//                    et_login_pwd.setInputType( InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
//                    image.setImageResource( R.drawable.login_icon_kejian );
//                    eyeOpen = true ;
//                }
//            }
//        });



    }



    private void LoadLogin(){


        if(Contig.phone.equals("")){
            Toast.makeText(LoginActivity.this,"请输入手机号", Toast.LENGTH_SHORT).show();
        }else if(Contig.pwd.equals("")){
            Toast.makeText(LoginActivity.this,"请输入密码", Toast.LENGTH_SHORT).show();
        }
        Log.e("","WYBS"+AUniqueIdentifier.getDeviceId(LoginActivity.this));
        String url = "http://develop.jhjvip.cn:8080/mall/mobile/login/in"+"?phone="+Contig.phone+"&pwd="+Contig.pwd+"&appId="+ AUniqueIdentifier.getDeviceId(LoginActivity.this);
        Log.e("","登录url===="+url);
        OkHttpUtils.get().url(url).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                LoginBean list = gson.fromJson(response, LoginBean.class);
                if(list.getCode().equals("10000")) {
//                    if (list != null && !TextUtils.isEmpty(list.getData().getToken())) {
//                        SPUtils.getInstance().put(SPConstants.KEY_USER_TOKEN, list.getData().getToken());
//                        SPUtils.getInstance().put(SPConstants.KEY_ISSTORE,list.getIsStore());
                        SPUtils.getInstance().put(SPConstants.KEY_USERID,list.getData().getUserid());
//                    }

                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//                    Log.e("",""+SPUtils.getInstance().getString(SPConstants.KEY_ISSTORE));
                    Intent intent=new Intent(LoginActivity.this,RankingRanNo1HtmlActivity.class);
                    intent.putExtra("url","http://wap.uhuijia.com.cn/ywym/index.html?userid="+list.getData().getUserid());
                    intent.putExtra("title","d");
                    startActivity(intent);
                    finish();
                }else {
//                    SPUtils.getInstance().put(SPConstants.KEY_ISSTORE,list.getIsStore());
                    Toast.makeText(LoginActivity.this, list.getMsg(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }





    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            case R.id.tv_SMS_quick_login:
//                Intent intentsms=new Intent(LoginActivity.this,SMSQuickLoginActivity.class);
//                startActivity(intentsms);
//                break;
//            case R.id.tv_Forgot_password_login:
//                Intent intentpass=new Intent(LoginActivity.this,ForgotPasswordLoginActivity.class);
//                startActivity(intentpass);
//                break;
//            case R.id.tv_login_Register:
//                Intent intenregister=new Intent(LoginActivity.this,RegisterLoginActivity.class);
//                startActivity(intenregister);
//                break;
        }
    }
}

